/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.gis.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.gis.domain.WeighbridgeIn;
import me.zhengjie.modules.gis.repository.WeighbridgeInRepository;
import me.zhengjie.modules.gis.service.dto.WeighbridgeInDto;
import me.zhengjie.modules.gis.service.dto.WeighbridgeInQueryCriteria;
import me.zhengjie.modules.gis.service.mapstruct.WeighbridgeInMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* 地磅流入接口服务
**/
@Service
@RequiredArgsConstructor
public class WeighbridgeInService {

    private final WeighbridgeInRepository weighbridgeInRepository;
    private final WeighbridgeInMapper weighbridgeInMapper;


    public Map<String,Object> queryAll(WeighbridgeInQueryCriteria criteria, Pageable pageable){
        Page<WeighbridgeIn> page = weighbridgeInRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(weighbridgeInMapper::toDto));
    }

    public List<WeighbridgeInDto> queryAll(WeighbridgeInQueryCriteria criteria){
        return weighbridgeInMapper.toDto(weighbridgeInRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public WeighbridgeInDto findById(Long id) {
        WeighbridgeIn weighbridgeIn = weighbridgeInRepository.findById(id).orElseGet(WeighbridgeIn::new);
        ValidationUtil.isNull(weighbridgeIn.getId(),"WeighbridgeIn","id",id);
        return weighbridgeInMapper.toDto(weighbridgeIn);
    }

    @Transactional(rollbackFor = Exception.class)
    public WeighbridgeInDto create(WeighbridgeIn resources) {
        return weighbridgeInMapper.toDto(weighbridgeInRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(WeighbridgeIn resources) {
        WeighbridgeIn weighbridgeIn = weighbridgeInRepository.findById(resources.getId()).orElseGet(WeighbridgeIn::new);
        ValidationUtil.isNull( weighbridgeIn.getId(),"WeighbridgeIn","id",resources.getId());
        weighbridgeIn.copy(resources);
        weighbridgeInRepository.save(weighbridgeIn);
    }


    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            weighbridgeInRepository.deleteById(id);
        }
    }


    public void download(List<WeighbridgeInDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WeighbridgeInDto weighbridgeInDto : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("车牌号", weighbridgeInDto.getCarNo());
            map.put("卡号", weighbridgeInDto.getIcCode());
            map.put("货物名称", weighbridgeInDto.getProductName());
            map.put("发货单位", weighbridgeInDto.getSourceUnit());
            map.put("区域", weighbridgeInDto.getArea());
            map.put("运输单位", weighbridgeInDto.getTransportUnit());
            map.put("处置单位", weighbridgeInDto.getDisposeUnitName());
            map.put("进厂时间", weighbridgeInDto.getTareTimeStr());
            map.put("毛重（吨）", weighbridgeInDto.getGrossWeight());
            map.put("皮重(吨)", weighbridgeInDto.getTareWeight());
            map.put("扣减(吨)", weighbridgeInDto.getDeductWeight());
            map.put("净重(吨)", weighbridgeInDto.getNetWeight());
            map.put("出单人(创建人)", weighbridgeInDto.getWeighMan());
            map.put("车间名称", weighbridgeInDto.getProductToWhere());
           // map.put("数据类型()", transportationInHistory.getType());
            map.put("数据状态", weighbridgeInDto.getDataStatusStr());
            map.put("创建时间", weighbridgeInDto.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}